// import { Button } from 'antd-mobile'
import styles from './index.module.scss'
// import Icon from '@/components/Icon'

import { NavBar, Form, Input, Button, List, Toast } from 'antd-mobile'
import { useHistory } from 'react-router'
import { useDispatch } from 'react-redux'
import { login, send } from '@/store/acitons/login'
import { useEffect, useRef, useState } from 'react'
import { InputRef } from 'antd-mobile/es/components/input'
export default function Login() {
  const history = useHistory()
  const dispatch = useDispatch()
  const [time, setTime] = useState(0)
  // 获取form 实例
  const [form] = Form.useForm()
  const back = () => {
    history.go(-1)
  }
  type LoginForm = {
    mobile: string
    code: string
  }
  // 1.表单提交
  const onFinish = async (values: LoginForm) => {
    await dispatch(login(values))
    Toast.show({
      content: '登陆成功',
      duration: 1000,
      icon: 'success',
      afterClose() {
        history.replace('/home')
      },
    })
  }
  // 2. 获取验证码
  const mobileRef = useRef<InputRef>(null)
  const timeRef = useRef(-1)
  const getCode = async () => {
    if (time > 0) return
    //获取手机号
    const mobile = form.getFieldValue('mobile')
    console.log(mobile)
    // 判断手机号校验是否成功
    const Error = form.getFieldError('mobile')
    if (!mobile || Error.length > 0) {
      //没有手机号或者校验失败
      mobileRef.current?.focus()
      return
    }
    // 校验成功
    await dispatch(send(mobile))
    // 3. 倒计时
    setTime(5)
    timeRef.current = window.setInterval(() => {
      setTime((time) => time - 1)
    }, 1000)
  }

  //
  useEffect(() => {
    if (time === 0) {
      clearInterval(timeRef.current)
    }
  }, [time])
  // 组件销毁的时候 清除定时器
  useEffect(() => {
    return () => {
      clearInterval(timeRef.current)
    }
  }, [])
  return (
    <div className={styles.root}>
      <NavBar onBack={back}></NavBar>
      <div className="login-form">
        <h2 className="title">账号登录</h2>

        <Form
          form={form}
          autoComplete="off"
          validateTrigger={['onChange', 'onBlur']}
          onFinish={onFinish}
          initialValues={{
            mobile: '13911111133',
            code: '246810',
          }}
        >
          <Form.Item
            className="login-item"
            name="mobile"
            rules={[
              { required: true, message: '手机号不能为空' },
              { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误' },
            ]}
          >
            <Input ref={mobileRef} placeholder="请输入手机号" maxLength={11} />
          </Form.Item>

          <List.Item
            className="login-code-extra"
            extra={
              <span className="code-extra" onClick={getCode}>
                {time === 0 ? '发送验证码' : `${time}s之后再次发送`}
              </span>
            }
          >
            <Form.Item
              className="login-item"
              name="code"
              rules={[
                { required: true, message: '验证码不能为空' },
                { pattern: /^\d{6}$/, message: '验证码格式错误' },
              ]}
            >
              <Input placeholder="请输入验证码" />
            </Form.Item>
          </List.Item>

          <Form.Item>
            <Button
              block
              type="submit"
              color="primary"
              className="login-submit"
            >
              登录
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  )
}
